package com.kehao.leetcode.jzoffer;

import org.junit.Test;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/10 9:41
 */
public class JZSolution012 {
    public int pivotIndex(int[] nums) {
        int sum = 0;
        for (int i = 1; i < nums.length; i++) {
            sum+=nums[i];
        }
        int left = 0,right = sum;
        if(left == right){
            return 0;
        }
        for (int pos = 1; pos < nums.length; pos++) {
            left+=nums[pos-1];
            right-=nums[pos];
            if(left == right){
                return pos;
            }
        }
        return -1;
    }

    @Test
    public void test01(){
        int[] nums = {1,7,3,6,5,6};
        System.out.println(pivotIndex(nums));
    }
}
